﻿using System.Collections.Generic;
using UnityEngine;
using FairyGUI;

public class FBiologyListMediator : MyWindow {
    GList _list;
    FEditBiologyMediator _editBiologyPanel;
    FCardTypePopup _biologyTypePopup;
    GComponent _biologyList;
    GTextField _biologyNum;

    static List<Biology> _biologyDataList;//所有生物数据
    static List<Biology> _curBiologyDataList;//筛选后的生物数据
    static FBiologyListMediator _instance;
    OneDelegate _callback;

    protected override void OnInit(){
        base.OnInit();
        this.contentPane = UIPackage.CreateObject("BiologyPackage", "BiologyList").asCom;
        _biologyList = this.contentPane;
        InitPanel();
    }

    public FBiologyListMediator(OneDelegate cb = default(OneDelegate))
    {
        _instance = this;

        SetCallBack(cb);
    }

    public void SetCallBack(OneDelegate cb)
    {
        _callback = cb;
        FreshCallBackPanel();
    }

    private void FreshCallBackPanel(){
        if (_biologyList == null) return;
        var closeBtn = _biologyList.GetChild("closeBtn");
        if(_callback != null)
        {
            closeBtn.onClick.Add(OnCloseBtn);
            closeBtn.visible = true;
        }else{
            closeBtn.visible = false;
        }
    }

    private void InitPanel()
    {
        FreshCallBackPanel();
        _biologyNum = _biologyList.GetChild("numText").asTextField;
        _biologyList.GetChild("newBtn").onClick.Add(OnNewBtnClick);
        var biologyTypeBtn = _biologyList.GetChild("biologyTypeBtn");
        biologyTypeBtn.onClick.Add(OnTypeBtnClick);
        biologyTypeBtn.visible = true;
        _list = _biologyList.GetChild("biologyList").asList;
        //编辑生物面板
        _editBiologyPanel = (FEditBiologyMediator)FUIMain.GetWindow(GMConfig.PanelType.EditBiologyPanel,null);
        //生物类型选择
        UIObjectFactory.SetPackageItemExtension("ui://CardPackage/CardTypePopup", typeof(FCardTypePopup));
        _biologyTypePopup = (FCardTypePopup)UIPackage.CreateObject("CardPackage", "CardTypePopup");

        InitData();
    }
    private static void InitData()
    {
        if (_biologyDataList != null) return;
        _biologyDataList = BiologyMO.GetBiologyList();
    }
	// Use this for initialization
    protected override void OnShown()
    {         
        //设置item单机事件监听
        _list.onClickItem.Add(OnBiologyListClick);
        _list.itemRenderer = RenderListItem;

        //赋值
        var typeDict = _editBiologyPanel.GetTypeDict();
        _biologyTypePopup.InitData(typeDict.GetValue("typeTextList"),FreshBiologyList);

        FreshBiologyList();
    }

    void RenderListItem(int index, GObject item)
    {
        var btn = (FBiology)item;
        btn.InitData(_curBiologyDataList[index]);
    }

    private void OnBiologyListClick(EventContext context)
    {
        var index = _list.GetChildIndex((GObject)context.data);
        if (_callback != null)
        {
            _callback(_curBiologyDataList[index].DeepClone());
        }
        else
        {
            _editBiologyPanel.Show();
            //获取对应所有生物内的下标，以便后面保存
            var _index = _biologyDataList.IndexOf(_curBiologyDataList[index]);
            _editBiologyPanel.InitData(_curBiologyDataList[index], _index);
        }
    }
    /// <summary>
    /// 新建生物
    /// </summary>
    private void OnNewBtnClick()
    {
        //创建一张新卡
        var _biology = new Biology();
        int lastId = 0;
        if(_biologyDataList.Count != 0)
        {
            var lastBiology = _biologyDataList[_biologyDataList.Count - 1];
            lastId = System.Convert.ToInt32(lastBiology.id.Split('_')[1]);
        }
        _biology.id = "Biology_"+(lastId + 1);
        BiologyMO.ResetBiologyName(_biology);
        BiologyMO.ResetBiologyDescript(_biology);
        _editBiologyPanel.Show();
        _editBiologyPanel.InitData(_biology,_biologyDataList.Count);
    }
    /// <summary>
    /// 类型筛选
    /// </summary>
    /// <param name="context"></param>
    private void OnTypeBtnClick(EventContext context)
    {
        var sender = (GComponent)context.sender;

        switch (sender.name)
        {
            case "biologyTypeBtn":
                GRoot.inst.ShowPopup(_biologyTypePopup, sender);
                break;
        }
    }
    /// <summary>
    /// 保存生物
    /// </summary>
    /// <param name="biologyData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveBiology(Biology biologyData,int index)
    {
        BiologyMO.SaveBiology(biologyData,index);
        if(_instance != null) _instance.FreshBiologyList();
        return true;
    }
    /// <summary>
    /// 删除生物
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelBiology(int index){
        if (index < 0 || index >= _biologyDataList.Count) return false;
        BiologyMO.DelBiology(index);
        if(_instance != null) _instance.FreshBiologyList();
        return true;
    }
    /// <summary>
    /// 刷新生物面板
    /// </summary>
    private void FreshBiologyList(){
        var biologyTypeSelectedList = _biologyTypePopup.GetSelectedList();

        _curBiologyDataList = new List<Biology>();

        for(int j = 0;j < _biologyDataList.Count; j++)
        {
            //生物类型筛选
            var biologyTypeOk = biologyTypeSelectedList.Count == 0 || biologyTypeSelectedList.Contains((int)_biologyDataList[j].type);
            if (biologyTypeOk) _curBiologyDataList.Add(_biologyDataList[j]);
        }
        //全部都没选择
        if(biologyTypeSelectedList.Count == 0)
        {
            _curBiologyDataList = _biologyDataList;
        }
        // _list.RemoveChildrenToPool();
        _list.numItems = _curBiologyDataList.Count;
        _biologyNum.text = _curBiologyDataList.Count.ToString();
    }
	// Update is called once per frame
	void Update () {
		
	}
    /// <summary>
    /// 关闭面板
    /// </summary>
    private void OnCloseBtn()
    {
        if (_callback != null) _callback(0);
    }
}
